package com.tjbank.cssys.web.controller;

import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkOrderLoanIntelligenceDivision;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOrderLoanIntelligenceDivisionService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = "CkOrderLoanIntelligenceDivisionController", description = "分案管理")
@RestController
@RequestMapping("/division")
public class CkOrderLoanIntelligenceDivisionController {
    @Autowired
    private CkOrderLoanIntelligenceDivisionService ckOrderLoanIntelligenceDivisionService;
    @Autowired
    private CkLogService ckLogService;
  private static  final   Logger logger = LoggerFactory.getLogger(CkOrderLoanIntelligenceDivision.class);

    @ApiOperation("获取分案列表或分案还款查询管理")
    @PostMapping("/list")
    public CommonResult<PageInfo> getDivisionList(@RequestBody Map<String, String> params, HttpServletRequest request){
        PageInfo pageInfo= ckOrderLoanIntelligenceDivisionService.selectDivisionList(params,request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取分案列表或分案还款查询管理");

        if(ckOrderLoanIntelligenceDivisionService!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pageInfo);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }
    }
    @ApiOperation("获取分案列表查询管理基本信息")
    @PostMapping("/getDivisionListBasics")
    public CommonResult<PageInfo> getDivisionListBasics(@RequestBody Map<String, String> params, HttpServletRequest request){
        PageInfo pageInfo= ckOrderLoanIntelligenceDivisionService.getDivisionListBasics(params,request);



        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取分案历史查询列表");

        if(ckOrderLoanIntelligenceDivisionService!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pageInfo);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }

    }
    @ApiOperation("获取分案列表详情页")
    @PostMapping("/getDivisionListDetails")
    public CommonResult getDivisionListDetails(@RequestBody Map<String, Object> params, HttpServletRequest request){
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取分案列表详情页");

        String peopleId = String.valueOf(params.get("peopleId"));
        String contractNum =String.valueOf(params.get("contractNum")) ;
        if (StringUtils.isNotEmpty(peopleId) &&StringUtils.isNotEmpty(contractNum) ) {
            Map<String, Object> ckOrderLoanDivisionTimesMap = ckOrderLoanIntelligenceDivisionService.getDivisionListDetails(params,request);

            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);

            return CommonResult.success(ckOrderLoanDivisionTimesMap);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("数据条件传输错误");
        }
    }
    @ApiOperation("获取分案还款查询管理基本信息")
    @PostMapping("/getRecoverBasics")
    public CommonResult<PageInfo> getRecoverBasics(@RequestBody Map<String, String> params, HttpServletRequest request){
        PageInfo pageInfo= ckOrderLoanIntelligenceDivisionService.getRecoverBasics(params,request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取分案还款查询管理基本信息");

        if(ckOrderLoanIntelligenceDivisionService!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pageInfo);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }

    }
    @ApiOperation("获取分案还款查询管理详细信息")
    @PostMapping("/getRecoverDetails")
    public CommonResult getRecoverDetails(@RequestBody Map<String, String> params, HttpServletRequest request){
        Map<String, Object> ckOrderLoanIntelligenceDivisionMap= ckOrderLoanIntelligenceDivisionService.getRecoverDetails(params,request);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("获取分案还款查询管理详细信息");

        if(ckOrderLoanIntelligenceDivisionMap!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(ckOrderLoanIntelligenceDivisionMap);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }

    }
    @ApiOperation("获取分案详情")
    @GetMapping("/getDivisionInfo")
    public CommonResult<CkOrderLoanIntelligenceDivision> getCkOrderLoanInterll(@RequestParam("id")int id ,HttpServletRequest request){
          CkOrderLoanIntelligenceDivision ckOrderLoanIntelligenceDivision =ckOrderLoanIntelligenceDivisionService.getCkOrderLoanDivionById(id);


        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams(id+"");
        ckLog.setOperationType("获取分案详情");

        if(ckOrderLoanIntelligenceDivision!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult .success(ckOrderLoanIntelligenceDivision);
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }

    }

    @ApiOperation("人工手动更改分案下发渠道")
    @PostMapping("/update/division/target")
    public CommonResult updateDivisionTarget(@RequestParam("id")String divisionIds,
                                             @RequestParam("n_outbound")Byte n_outbound,
                                             @RequestParam("status")Byte status,
                                            @RequestParam(value="model_type",required = false)String model_type ,HttpServletRequest request){


        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams(divisionIds+n_outbound+status+model_type);
        ckLog.setOperationType("人工手动更改分案下发渠道");

        try {
            boolean b = ckOrderLoanIntelligenceDivisionService.updateAndToLog(divisionIds, n_outbound, model_type,status);
            if (b) {
                ckLog.setResult("success:200");
                ckLogService.addInsert(ckLog);
                return CommonResult.success(null);
            } else {
                ckLog.setResult("error:500");
                ckLogService.addInsert(ckLog);
                return  CommonResult .failed("再次分案不成功");
            }
        } catch (Exception e) {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult .failed("服务器错误");
        }
    }
}
